/**
 * Project: springyounger.lottery.biz.user
 * 
 * File Created at 2010-3-27
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyounger.lottery.service.user;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.springyounger.lottery.dal.dao.QuestionDao;
import com.springyounger.lottery.dal.dataobject.LotteryPhaseQuestion;
import com.springyounger.lottery.dal.dataobject.LotteryQuestion;
import com.springyounger.lottery.dal.dataobject.LotteryQuestionResult;
import com.springyounger.lottery.dal.dataobject.ResultInfo;

/**
 * QuestionService
 * 
 * @author springyounger
 */
public class QuestionService {
    private Logger              logger = Logger.getLogger(QuestionService.class);
    private QuestionDao         questionDao;
    private TransactionTemplate transactionTemplate;

    /**
     * 获取活动题目
     * 
     * @param question
     * @return
     */
    public LotteryPhaseQuestion getPhaseQuestion(int phaseId, Date publishDate, String publishSn,
                                                 String questionType) {
        try {
            LotteryPhaseQuestion question = new LotteryPhaseQuestion();
            question.setPhaseId(phaseId);
            question.setPublishDate(publishDate);
            question.setPublishSn(publishSn);
            question.setQuestionType(questionType);
            return questionDao.getPhaseQuestion(question);
        } catch (Exception e) {
            logger.error("获取活动题目失败", e);
            return null;
        }
    }

    /**
     * 根据questionId获取答案列表
     * 
     * @param questionId
     * @return
     */
    public List<LotteryQuestionResult> getQuestinResult(int questionId) {
        try {
            return questionDao.getQuestionResult(questionId);
        } catch (Exception e) {
            logger.error("获取答案列表失败!", e);
            return null;
        }
    }

    /**
     * 根据resultId获取答案
     * 
     * @param resultId
     * @return
     */
    public LotteryQuestionResult getQuestionResult(int resultId) {
        try {
            return questionDao.getQuestionResultByResultId(resultId);
        } catch (Exception e) {
            logger.error("获取答案失败!", e);
            return null;
        }
    }

    /**
     * 根据questionIdh获取活动题目
     * 
     * @param questionId
     * @return
     */
    public LotteryPhaseQuestion getPhaseQuestion(int questionId,int phaseId) {
        try {
            return questionDao.getPhaseQuestion(questionId,phaseId);
        } catch (Exception e) {
            logger.error("根据questionId获取活动题目失败!", e);
            return null;
        }
    }

    /**
     * 获取题目总数
     * 
     * @return
     */
    public int getAllQuestionCount() {
        try {
            return questionDao.getAllQuestionCount();
        } catch (Exception e) {
            logger.error("获取题目总数失败!", e);
            return 0;
        }
    }

    /**
     * 获取所有题目
     * 
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<LotteryQuestion> getAllQuestion(int pageIndex, int pageSize) {
        try {
            return questionDao.getAllQuestion(pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("获取所有题目失败", e);
            return null;
        }
    }

    /**
     * 更新问题
     * 
     * @param question
     * @return
     */
    public boolean updateQuestion(LotteryQuestion question) {
        try {
            questionDao.updateQuestion(question);
            return true;
        } catch (Exception e) {
            logger.error("更新问题失败", e);
            return false;
        }
    }

    /**
     * 删除问题
     * 
     * @param questionId
     * @return
     */
    public boolean deleteQuestionById(int questionId) {
        try {
            questionDao.deleteQuestionById(questionId);
            return true;
        } catch (Exception e) {
            logger.error("删除问题失败", e);
            return false;
        }
    }

    /**
     * 获取题目
     * 
     * @param questionId
     * @return
     */
    public LotteryQuestion getQuestionById(int questionId) {
        try {
            return questionDao.getQuestionById(questionId);
        } catch (Exception e) {
            logger.error("获取题目失败!", e);
            return null;
        }
    }

    /**
     * 根据questionId获取正确答案
     * 
     * @param questionId
     * @return
     */
    public String getResultById(int questionId) {
        try {
            return questionDao.getResultById(questionId);
        } catch (Exception e) {
            logger.error("根据questionId获取正确答案失败", e);
            return null;
        }
    }

    /**
     * 创建题目和答案
     * 
     * @param question
     * @return
     */
    public ResultInfo insertQuestionAndResult(final LotteryQuestion question,
                                              final List<LotteryQuestionResult> resultList) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo = (ResultInfo) getTransactionTemplate().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status) {
                //事务处理
                ResultInfo info = new ResultInfo();
                LotteryQuestion insertQuestion = new LotteryQuestion();
                try {
                    insertQuestion = questionDao.insertQuestion(question);
                    info.setObject(insertQuestion);
                } catch (Exception e) {
                    logger.error("创建题目失败!", e);
                    info.setType(ResultInfo.RESULT_FAILED);
                    status.setRollbackOnly();
                }
                try {
                    //设置返回的questionId
                    questionDao.batchInsertQuestionResult(resultList, insertQuestion
                            .getQuestionId());
                } catch (Exception e) {
                    logger.error("创建答案失败", e);
                    info.setType(ResultInfo.RESULT_FAILED);
                    status.setRollbackOnly();
                }
                return info;
            }
        });
        return resultInfo;
    }

    /**
     * 批量更新 答案
     * 
     * @param question
     * @param resultList
     * @return
     */
    public ResultInfo updateQuestionAndResult(final LotteryQuestion question,
                                              final List<LotteryQuestionResult> resultList) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo = (ResultInfo) getTransactionTemplate().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status) {
                //事务处理
                ResultInfo info = new ResultInfo();
                try {
                    questionDao.updateQuestion(question);
                } catch (Exception e) {
                    logger.error("更新题目失败!", e);
                    info.setType(ResultInfo.RESULT_FAILED);
                    status.setRollbackOnly();
                }
                try {
                    //设置返回的questionId
                    questionDao.batchUpdateQuestionResult(resultList, question.getQuestionId());
                } catch (Exception e) {
                    logger.error("更新答案失败", e);
                    info.setType(ResultInfo.RESULT_FAILED);
                    status.setRollbackOnly();
                }
                return info;
            }
        });
        return resultInfo;
    }

    /**
     * 获取已存在题目
     * 
     * @param content
     * @return
     */
    public boolean isExistName(String content) {
        try {
            boolean isExist = false;
            if (questionDao.isExistName(content) > 0) {
                isExist = true;
            }
            return isExist;
        } catch (Exception e) {
            logger.error("获取已存在题目失败", e);
            return false;
        }
    }

    /**
     * 批量创建活动题目
     * 
     * @param phaseQuestionList
     * @return
     */
    public boolean batchInsertPhaseQuestion(final List<LotteryPhaseQuestion> phaseQuestionList) {
        try {
            questionDao.batchInsertPhaseQuestion(phaseQuestionList);
            return true;
        } catch (Exception e) {
            logger.error("批量创建活动题目失败", e);
            return false;
        }
    }

    /**
     * 根据id获取题目总数
     * 
     * @param questionType
     * @return
     */
    public int getAllQuestionCountByType(String questionType) {
        try {
            return questionDao.getAllQuestionCountByType(questionType);
        } catch (Exception e) {
            logger.error("根据type获取题目总数失败", e);
            return 0;
        }
    }

    /**
     * 根据type获取全部题目
     * 
     * @param questionType
     * @return
     */
    public List<LotteryQuestion> getAllQuestionByType(String questionType) {
        try {
            return questionDao.getAllQuestionByType(questionType);
        } catch (Exception e) {
            logger.error("根据type获取全部题目失败", e);
            return null;
        }
    }

    /**
     * 获取活动题目数量
     * 
     * @param phaseId
     * @return
     */
    public boolean getPhaseQuestionCount(int phaseId) {
        try {
            if (questionDao.getPhaseQuestionCount(phaseId) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error("获取活动题目数量失败", e);
            return false;
        }
    }

    /**
     * 获取活动题目总数
     * 
     * @return
     */
    public int getAllPhaseQuestionCount(int phaseId) {
        try {
            return questionDao.getPhaseQuestionCount(phaseId);
        } catch (Exception e) {
            logger.error("获取活动题目总数失败", e);
            return 0;
        }
    }

    /**
     * 获取活动题目列表
     * 
     * @param phaseId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<LotteryPhaseQuestion> getAllPhaseQuestion(int phaseId, int pageIndex, int pageSize) {
        try {
            return questionDao.getAllPhaseQuestion(phaseId, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("获取活动题目列表失败", e);
            return null;
        }
    }
    
    /**
     * @param questionDao the questionDao to set
     */
    public void setQuestionDao(QuestionDao questionDao) {
        this.questionDao = questionDao;
    }

    /**
     * @param transactionTemplate the transactionTemplate to set
     */
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    /**
     * @return the transactionTemplate
     */
    public TransactionTemplate getTransactionTemplate() {
        return transactionTemplate;
    }
}
